package com.zhejiangzhengyuan.municipal_pumping_station.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.WarnEvent;
import com.zhejiangzhengyuan.municipal_common.entity.dto.NewCase;
import com.zhejiangzhengyuan.municipal_common.entity.dto.ReceiptCaseDto;
import com.zhejiangzhengyuan.municipal_common.entity.dto.SendCase;
import com.zhejiangzhengyuan.municipal_common.entity.vo.CaseDetails;
import com.zhejiangzhengyuan.municipal_common.entity.vo.CaseIsHandledVo;
import com.zhejiangzhengyuan.municipal_common.entity.vo.CaseNoHandle;
import com.zhejiangzhengyuan.municipal_common.entity.vo.CaseTrackVo;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.*;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;

/**
 * @author :linju
 * @create on : 2019/12/11
 */
@RestController
@Slf4j
@RequestMapping("/station/pumpStationCaseManagement")
@Api(tags = "泵站案件管理")
public class PumpCaseManagementController {
    private final String warnType=ConstantUtil.WARN_TYPE.PUMPING_STATION;
    private final String caseType = "泵站案件";
    private final String moduleName = "municipal_pumping_station";

    @Autowired private CaseManagementService caseManagementService;
    @Autowired private WarnEventService warnEventService;
    @Autowired private CaseService caseService;
    @Autowired private InspectionTasksService inspectionTasksService;
    @Autowired private BIService biService;


    @ApiOperation("查询泵站在未派单时的数据 分页查询")
    @GetMapping("/selectPumpCaseNoHandlePage")
    public ResponseVo selectPumpCaseNoHandlePage(@ApiParam("页码") Long pageNo, @ApiParam("每页显示条数") Long pageSize
     ,String dataOrigin,
      String reason,
      String username,
      String startTimestamp,
      String endTimestamp) {
        // pageNo默认为1；size默认为15
        pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize == null || pageSize <= 0 ? 15 : pageSize;
        Page<CaseNoHandle> page = new Page<>(pageNo, pageSize);
        Page<CaseNoHandle> all = caseManagementService.selectPumpCaseNoHandlePage(page, dataOrigin,reason,username,startTimestamp,endTimestamp,warnType);
        return ResponseVo.successWithData(all);
    }
    @ApiOperation("泵站案件复位")
    @PostMapping("/updatePumpWarnEvent")
    public ResponseVo updatePumpWarnEvent(@RequestBody WarnEvent warnEvent) {
        if (warnEvent.getUpdateTime() == null) {
            return new ResponseVo(ConstantUtil.ResultCode.CLIENT_PARAM_ERROR);
        }
        return warnEventService.updateWarnEvent(warnEvent) > 0
                ? ResponseVo.SUCCESS
                : ResponseVo.SERVER_ERROR;
    }

    @ApiOperation("泵站案件派单")
    @PostMapping("/sendPumpCase")
    public ResponseVo sendPumpCase(@Validated @ModelAttribute SendCase sendCase) throws IOException {
        return caseService.saveCase(sendCase,caseType,moduleName);
    }

    @ApiOperation("新建泵站案件")
    @PostMapping("/newPumpCase")
    public ResponseVo newPumpCase(@Validated @ModelAttribute NewCase newCase) throws IOException {
        return caseManagementService.insertCaseAndWarn(newCase,moduleName,caseType,warnType);
    }

    @ApiOperation("生成告警事件图表")
    @RequestMapping(value = "/pumpWarnEventChart", method = RequestMethod.GET)
    public ResponseVo pumpWarnEventChart(
            @ApiParam(value = "开始时间 格式为 年-月-日", required = true)
            @NotBlank(message = "startTime 不能为null,格式为 年-月-日")
                    String startTime,
            @ApiParam(value = "结束时间 格式为 年-月-日", required = true)
            @NotBlank(message = "endTime 不能为null,格式为 年-月-日")
                    String endTime) {
        return warnEventService.warnEventChart(startTime, endTime);
    }

    @ApiOperation("查询泵站案件在处理中的数据 分页查询")
    @GetMapping("/selectPumpCaseHandlingPage")
    public ResponseVo selectPumpCaseHandlingPage(@ApiParam("页码") Long pageNo, @ApiParam("每页显示条数") Long pageSize
    ,String dataOrigin,
     String reason,
     String username,
     String startTimestamp,
     String endTimestamp) {
        // pageNo默认为1；size默认为15
        pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize == null || pageSize <= 0 ? 15 : pageSize;
        Page<CaseNoHandle> page = new Page<>(pageNo, pageSize);
        Page<CaseNoHandle> all = caseManagementService.selectPumpCaseHandlingPage(page, dataOrigin,reason,username,startTimestamp,endTimestamp,caseType,warnType);
        return ResponseVo.successWithData(all);
    }

    @ApiOperation("泵站案件回执")
    @PostMapping("/receiptCase")
    public ResponseVo receiptCase(@Validated @ModelAttribute ReceiptCaseDto receiptCaseDto){
        long i = caseService.receiptCase(receiptCaseDto,moduleName);
        return ResponseVo.successWithData(i);
    }

    @ApiOperation("泵站案件回执打回")
    @GetMapping("/receiptBackCase")
    public ResponseVo receiptCase(Long cid) {
        int i = caseService.repeatReceipt(cid);
        return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
    }

    @ApiOperation("泵站案件从处理中转向已处理")
    @GetMapping("/caseHandingTurnToCaseIsHandled")
    public ResponseVo caseHandingTurnToCaseIsHandled(Long cid) {
        int i = caseService.caseHandingTurnToCaseIsHandled(cid);
        return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
    }

    @ApiOperation("重新派单接口")
    @PostMapping("/repeatSendPumpCase")
    public ResponseVo repeatSendPumpCase(@Validated @ModelAttribute SendCase sendCase) throws IOException {
        return caseService.saveCase(sendCase,caseType,moduleName);
    }

    @ApiOperation("派单新建多文件上传")
    @PostMapping("/uploadToFile")
    public ResponseVo uploadToFile(long cid, MultipartFile file) throws IOException {
        if(file==null){
            return ResponseVo.CLIENT_PARAM_ERROR;
        }
        int i = caseService.uploadToFile(cid, file,moduleName);
        return i>0? ResponseVo.SUCCESS:ResponseVo.SERVER_ERROR;
    }

    @ApiOperation("回执多文件上传")
    @PostMapping("/uploadReceiptToFile")
    public ResponseVo uploadReceiptToFile(long rid, MultipartFile file) throws IOException {
        int i = caseService.uploadReceiptToFile(rid, file,moduleName);
        return i>0? ResponseVo.SUCCESS:ResponseVo.SERVER_ERROR;
    }


    @ApiOperation("泵站案件追踪，需要传入当前案件的 cid")
    @GetMapping("/selectPumpCaseTrack")
    public ResponseVo selectPumpCaseTrack(Long id) {
        CaseTrackVo caseTrackVo = caseManagementService.selectCaseTrack(id);
        return caseTrackVo == null
                ? ResponseVo.NOT_FOUND_RESOURCES
                : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, caseTrackVo);
    }

    @ApiOperation("查询泵站案件在已处理的数据 分页查询")
    @GetMapping("/selectPumpCaseIsHandledPage")
    public ResponseVo selectPumpCaseIsHandledPage(Long pageNo,Long pageSize
    ,String dataOrigin,
    String reason,
    String username,
    String startTimestamp,
    String endTimestamp) {
        // pageNo默认为1；size默认为14
        pageNo = pageNo == null || pageNo < 1 ? 1L : pageNo;
        pageSize=pageSize==null||pageSize<0? 1L:pageSize;
        Page<CaseIsHandledVo> page = new Page<>(pageNo, pageSize);
        Page<CaseIsHandledVo> voPage =
                caseManagementService.selectPumpCaseIsHandledPage(page, dataOrigin,reason,username,startTimestamp,endTimestamp,caseType, warnType);
        List<CaseIsHandledVo> records = voPage.getRecords();
        if (records!=null) {
            for (CaseIsHandledVo record : records) {
                CaseIsHandledVo handledPageName =
                        caseManagementService.selectCaseIsHandledPageName(record.getCid(), caseType);
                record.setName(handledPageName.getName());
            }
        }
        return ResponseVo.successWithData(voPage);
    }


    @ApiOperation("根据泵站案件ID 警报时间ID 设备ID 展示案件详情页")
    @GetMapping("/selectPumpCaseDetailsById")
    public ResponseVo selectPumpCaseDetailsById(Long wid, Long cid, Long did) {
        CaseDetails caseDetails = caseManagementService.selectCaseDetailsById(wid, cid, did);
        return caseDetails == null ? ResponseVo.SERVER_ERROR : ResponseVo.successWithData(caseDetails);
    }

    @ApiOperation("手机端 泵站专题展示")
    @GetMapping("/getPumpData")
    public ResponseVo getPumpData(Integer month) {
        return biService.getPumpData(warnType,month,null);
    }


}
